package com.example.questions.service.impl;

import com.example.questions.dao.InterviewQuestionsDao;
import com.example.questions.entity.InterviewQuestions;
import com.example.questions.service.InterviewQuestionsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (InterviewQuestions)表服务实现类
 *
 * @author makejava
 * @since 2023-07-01 10:06:16
 */
@Service("interviewQuestionsService")
public class InterviewQuestionsServiceImpl implements InterviewQuestionsService {
    @Resource
    private InterviewQuestionsDao interviewQuestionsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id  主键
     * @return 实例对象
     */
    @Override
    public InterviewQuestions queryById(Integer id) {
        return this.interviewQuestionsDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param interviewQuestions 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo<InterviewQuestions> queryByPage(InterviewQuestions interviewQuestions, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<InterviewQuestions> questionsList = this.interviewQuestionsDao.queryAll(interviewQuestions);
        return new PageInfo<>(questionsList);
    }

    /**
     * 新增数据
     *
     * @param interviewQuestions 实例对象
     * @return 实例对象
     */
    @Override
    public InterviewQuestions insert(InterviewQuestions interviewQuestions) {
        interviewQuestions.setCreateTime(new Date());
        interviewQuestions.setIsDeleted(0);
        this.interviewQuestionsDao.insert(interviewQuestions);
        return interviewQuestions;
    }

    /**
     * 修改数据
     *
     * @param interviewQuestions 实例对象
     * @return 实例对象
     */
    @Override
    public InterviewQuestions update(InterviewQuestions interviewQuestions) {
        interviewQuestions.setUpdateTime(new Date());
        this.interviewQuestionsDao.update(interviewQuestions);
        return this.queryById(interviewQuestions.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id ) {
        return this.interviewQuestionsDao.deleteById(id) > 0;
    }

    @Override
    public Integer queryCount() {
        return Math.toIntExact(this.interviewQuestionsDao.count(new InterviewQuestions()));
    }
}
